﻿using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;
using Habi.Solver.Common;
using Habi.Solver.Models;
using Habi.Solver.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotNetExtensions.AspNet.DevExpress.Services;
using DotNetExtensions.AspNet.DevExpress.MVVM.ViewModels;

namespace Habi.Solver.ViewModels.Meta
{
    public class LoginViewModel: SingleObjectViewModel<UserModel, string>
    {
        public IMessageService Notify { get { return this.GetService<IMessageService>(); } }
        public IDocumentManagerService FlyoutDocumentManagerService
        {
            get { return this.GetService<IDocumentManagerService>("FlyoutDocumentManagerService"); }
        }
        public virtual string UserId { get; set; }
        public virtual string UserPwd { get; set; }

        private IList<UserModel> MockUserList;

        public LoginViewModel()
        {
            MockUserList = new List<UserModel>
            {
                new UserModel { Id = 1, Name = "黄兄", Host = "192.168.20.217" },
                new UserModel { Id = 2, Name = "李兄", Host = "192.168.20.218" },
                new UserModel { Id = 3, Name = "杨兄", Host = "192.168.20.219" },
                new UserModel { Id = 4, Name = "郭兄", Host = "192.168.20.220" },
            };
        }
        
        public void Enter()
        {
            if (string.IsNullOrEmpty(UserId) || string.IsNullOrEmpty(UserPwd))
            {
                Notify.Show("Please type you Id & Pwd.");
            }
            else
            {
                var user = MockUserList.Where(c => c.Name == UserId || c.Id.ToString() == UserId);
                if (!user.Any())
                {
                    Notify.Show("User not registed");
                }
                else
                {
                    AppEnvironment.Default.User.Update(user.First().Id, user.First().Name);
                    Messenger.Default.Send(AppEnvironment.Default.User);
                    Close();
                }
            }
        }

        public void RegistNew()
        {
            NavigateTo(MainViewModel.RegsitView, true);
        }

        public void Quit()
        {

        }
    }
}
